import { Button, filter } from '@/components';
import Ajax from '@/utils/ajax';
import tool from '@/utils/tool';
import { Link, Tooltip } from 'antd';
import _ from 'lodash';
import React from 'react';
import ConfirmWrap from './confirmWrap';

export default class Action extends React.Component {
  //视图添加action解析
  static viewAction(view, action, ctx, actionCbk) {
    if (!action) {
      return view;
    }
    let onClick;
    if (_.isString(action)) {
      onClick = (e) => {
        return Ajax.post(tool.getValue(action, ctx)).then((res) => {
          actionCbk && actionCbk(ctx);
          return res;
        });
      };
    } else if (_.isFunction(action)) {
      onClick = (e) => {
        return action(e, ctx);
      };
    } else if (typeof action == 'object') {
      const {
        method = 'post',
        params,
        url,
        server,
        then,
        convert,
        convertParams,
      } = action;
      if (!url && !server) {
        return view;
      }

      onClick = (e) => {
        let _params = tool.getValue(params, ctx);
        if (convertParams) {
          _params = convertParams(_params);
        }

        let _server;
        if (server) {
          _server = server(_params);
        }
        if (url) {
          _server = Ajax[method](tool.getValue(url, ctx), _params);
        }

        let p = _server.then((res) => {
          if (convert) {
            return convert(res);
          }
          actionCbk && actionCbk(ctx);
          return res;
        });
        return p;
      };
    } else {
      return view;
    }
    return React.cloneElement(view, {
      onClick,
    });
  }

  //视图封装
  static viewWrap(props, viewFun, conf = {}) {
    const {
      ctx,
      confirm,
      confirmText,
      tooltip,
      url,
      params,
      action,
      path,
      actionCbk,
    } = props;

    const { linkConf, confirmConf, tooltipConf } = conf;

    let view = viewFun;
    if (_.isFunction(viewFun)) {
      view = viewFun();
    }

    if (action) {
      view = Action.viewAction(view, action, ctx, actionCbk);
    }

    if (path) {
      view = (
        <Link to={tool.getValue(path, ctx)} {...linkConf}>
          {view}
        </Link>
      );
    }

    if (confirm || confirmText) {
      view = (
        <ConfirmWrap
          confirmText={confirmText}
          title={confirm}
          placement="bottom"
          {...confirmConf}
        >
          {view}
        </ConfirmWrap>
      );
    }

    if (tooltip) {
      view = (
        <Tooltip title={tooltip} {...tooltipConf}>
          {view}
        </Tooltip>
      );
    }
    return view;
  }

  render() {
    const {
      ctx,
      confirm,
      confirmConf,
      confirmText,
      url,
      params,
      action,
      path,
      disabled,
      tooltipConf,
      Component = Button,
      visible = true,
      ...props
    } = this.props;

    let _visible = filter.parse(
      {
        name: 'and',
        data: visible,
      },
      ctx,
      (name, data) => {
        //处理方式存在变化
        if (name == 'change') {
          this.setState({});
        }
      },
    );

    if (!_visible) {
      return null;
    }

    let _disabled = filter.parse(
      { name: 'or', data: disabled },
      ctx,
      (name, data) => {
        if (name == 'change') {
          this.setState({});
        }
      },
    );

    return Action.viewWrap(
      this.props,
      <Component disabled={_disabled} {...props} />,
      {
        confirmConf,
        tooltipConf,
      },
    );
  }
}
